package edu.uc.ui.ctrl25;

import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.swing.text.DefaultEditorKit.BeepAction;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import com.liuvei.common.PagerItem;
import com.liuvei.common.SysFun;

import edu.uc.bean.Goods;
import edu.uc.bean.News;
import edu.uc.service.NewsService;

@Repository("NewsAction")
@Scope("prototype")
public class NewsAction extends BaseAction {
	private static final long serialVersionUID = 1L;

	@Autowired
	private NewsService newsService;
	private String id;
	private Long newsId;
	private Long catId;
	private String title;
	private String author;
	private String summary;
	private String picPath;
	private String status;
	private String remark;
	private Long sortNum;
	private Long isDeleted;
	private Long creatBy;
	private Long updateBy;
	private Timestamp createOn;
	private Timestamp updateOn;

	public String list() {
		/*
		 * PagerItem pagerItem = new PagerItem();
		pagerItem.parsePageSize(pageSize);
		pagerItem.parsePageNum(pageNum);
		//��ҳ �������� ���ҷ������������м�¼ 
		Long rowCount = goodsService.count();
		//��ҳ2 ����¼������pagerItem,�Ա���з�ҳ�ĸ������
		pagerItem.changeRowCount(rowCount);
		//��ҳ3 �����ݿ�ȡָ����ҳ������ 
		vList = goodsService.pager(pagerItem.getPageNum(),pagerItem.getPageSize());
		//��ҳ4 ����ҳ����ת��url
		pagerItem.changeUrl(SysFun.generalUrl(requestURI, queryString));
		//��ҳ5 ����ҳ���� �ŵ������� �Ա�ҳ����Է���
		request.put("pagerItem", pagerItem);
		request.put("dataList", vList);
		for(int i = 0; i<vList.size();i++){
			System.out.println(vList.get(i).getAddress());
		}
		 */
		List<News> newsList = new ArrayList<>();
		PagerItem pagerItem = new PagerItem();
		pagerItem.parsePageSize(pageSize);
		pagerItem.parsePageNum(pageNum);
		
		Long rowCount = newsService.count();
		
		pagerItem.changeRowCount(rowCount);
		System.out.println(pagerItem.getParamPageNum());
		System.out.println(pagerItem.getParamPageSize());
		newsList = newsService.pager(pagerItem.getPageNum(), pagerItem.getPageSize());
		
		pagerItem.changeUrl(SysFun.generalUrl(requestURI, queryString));
		request.put("pagerItem", pagerItem);
		request.put("dataList", newsList);
//		for (int i = 0; i < newsList.size(); i++) {
//			System.out.println(newsList.get(i).getTitle());
//		}
		return "list";
	}

	public String listDeal() throws UnsupportedEncodingException {
	
		request.put("title", title);
		if (!SysFun.isNullOrEmpty(title)) {
			title = title.trim();
		} else {
			title = "";
		}
//		System.out.println(title + 1111);
		List<News> newsList = null;
		
		PagerItem pagerItem = new PagerItem();
		pagerItem.parsePageSize(pageSize);
		pagerItem.parsePageNum(pageNum);
		
		Long rowCount =newsService.countByName(title);
		
		pagerItem.changeRowCount(rowCount);
		
		
		newsList = newsService.pagerByName(title, pagerItem.getPageNum(), pagerItem.getPageSize());
		
		pagerItem.changeUrl(SysFun.generalUrl(requestURI, queryString));
		request.put("pagerItem", pagerItem);
		request.put("dataList", newsList);
		return "list";
	}

	public String insert() {
		return "insert";
	}

	public String insertDeal() {
		request.put("title", title);
		request.put("author", author);
		String vMsg = "";
		if (SysFun.isNullOrEmpty(title)) {
			vMsg += "标题不能为空";
		}
		if (author == null) {
			vMsg += "作者不能为空";
		}
		if (!SysFun.isNullOrEmpty(vMsg)) {
			request.put("msg", vMsg);
			System.out.println(vMsg);
			return "insert";
		}
		News bean = new News();
		bean.setCatId(catId);
		bean.setTitle(title);
		bean.setAuthor(author);
		bean.setSummary(summary);
		bean.setPicPath(picPath);
		bean.setStatus(status);
		bean.setRemark(remark);
		bean.setSortNum(sortNum);
		bean.setIsDeleted(isDeleted);
		bean.setCreatBy(creatBy);
		bean.setUpdateBy(updateBy);
		bean.setCreateOn(createOn);
		bean.setUpdateOn(updateOn);
		Long result = 0L;
		try {
			result = newsService.insert(bean);
		} catch (Exception e) {
			// TODO: handle exception
			vMsg += "添加失败" + e.getMessage();
		}
		if (result > 0) {
			return "go_preload";
		} else {
			request.put("msg", vMsg);
			return "insert";
		}
	}

	public String delete() {
		return "go_ok";
	}

	public String deleteDeal() {
		if (!SysFun.isNullOrEmpty(id)) {
			Long iId = SysFun.parseLong(id);
			Long result = newsService.delete(iId);
			if (result > 0) {
				return "go_ok";
			}

		}
		return "go_no";
	}

	
	public String update() {
		if (!SysFun.isNullOrEmpty(id)) {
			Long iId = SysFun.parseLong(id);
			News bean = newsService.load(iId);
			if (bean != null) {
				System.out.println(bean.getNewsId());
				request.put("newsId", bean.getNewsId());
				request.put("catId", bean.getCatId());
				request.put("title", bean.getTitle());
				request.put("author", bean.getAuthor());
				request.put("summary", bean.getSummary());
				request.put("picPath", bean.getPicPath());
				request.put("status", bean.getStatus());
				request.put("remark", bean.getRemark());
				request.put("sortNum", bean.getSortNum());
				request.put("isDeleted", bean.getIsDeleted());
				request.put("creatBy", bean.getCreatBy());
				request.put("updateBy", bean.getUpdateBy());
				request.put("createOn", bean.getCreateOn());
				request.put("updateOn", bean.getUpdateOn());
				return "update";
			}
		}
		return "update";
	}

	public String updateDeal() {
		Long result = 0L;
		String vMsg = "";
		request.put("newsId", newsId);
		request.put("catId", catId);
		request.put("title", title);
		request.put("author", author);
		request.put("summary", summary);
		request.put("picPath", picPath);
		request.put("status", status);
		request.put("remark", remark);
		request.put("sortNum", sortNum);
		request.put("isDeleted", isDeleted);
		request.put("creatBy", creatBy);
		request.put("updateBy", updateBy);
		request.put("createOn", createOn);
		request.put("updateOn", updateOn);
		if (SysFun.isNullOrEmpty(title)) {
			vMsg += "标题不能为空";
		}
		if (SysFun.isNullOrEmpty(author.toString())) {
			vMsg += "作者不能为空";
		}
		if (!SysFun.isNullOrEmpty(vMsg)) {
			request.put("msg", vMsg);
			System.out.println(vMsg);
			return "update";
		}
		System.out.println(newsId + "-------------------");
		System.out.println();
		News bean = newsService.load(newsId);
		System.out.println(author + "111111");
		System.out.println(summary + "222222");

		if (bean == null) {
			vMsg += "没有找到相关数据";
		}
		if (!SysFun.isNullOrEmpty(vMsg)) {
			request.put("msg", vMsg);
			System.out.println(vMsg);
			return "update";
		}
		bean.setCatId(catId);
		bean.setTitle(title);
		bean.setAuthor(author);
		bean.setSummary(summary);
		bean.setPicPath(picPath);
		bean.setStatus(status);
		bean.setRemark(remark);
		bean.setSortNum(sortNum);
		bean.setIsDeleted(isDeleted);
		bean.setCreatBy(creatBy);
		bean.setUpdateBy(updateBy);
		bean.setCreateOn(createOn);
		bean.setUpdateOn(updateOn);
		try {
			result = newsService.update(bean);
		} catch (Exception e) {
			// TODO: handle exception
			vMsg += "更新失败" + e.getMessage();
		}
		if (result > 0) {
			return "go_preload";
		} else {
			request.put("msg", vMsg);
			return "update";
		}
	}

	public String detail() {

		if (!SysFun.isNullOrEmpty(id)) {
			String vMsg = "";
			Long iId = SysFun.parseLong(id);
			News bean = newsService.load(iId);
			if (bean != null) {
				request.put("catId", bean.getCatId());
				request.put("title", bean.getTitle());
				request.put("author", bean.getAuthor());
				request.put("summary", bean.getSummary());
				request.put("picPath", bean.getPicPath());
				request.put("status", bean.getStatus());
				request.put("remark", bean.getRemark());
				request.put("sortNum", bean.getSortNum());
				request.put("isDeleted", bean.getIsDeleted());
				request.put("creatBy", bean.getCreatBy());
				request.put("updateBy", bean.getUpdateBy());
				request.put("createOn", bean.getCreateOn());
				request.put("updateOn", bean.getUpdateOn());
				return "detail";
			} else {
				vMsg += "查询失败";
			}
		}
		return "detail";

	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Long getNewsId() {
		return newsId;
	}

	public void setNewsId(Long newsId) {
		this.newsId = newsId;
	}

	public Long getCatId() {
		return catId;
	}

	public void setCatId(Long catId) {
		this.catId = catId;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getSummary() {
		return summary;
	}

	public void setSummary(String summary) {
		this.summary = summary;
	}

	public String getPicPath() {
		return picPath;
	}

	public void setPicPath(String picPath) {
		this.picPath = picPath;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public Long getSortNum() {
		return sortNum;
	}

	public void setSortNum(Long sortNum) {
		this.sortNum = sortNum;
	}

	public Long getIsDeleted() {
		return isDeleted;
	}

	public void setIsDeleted(Long isDeleted) {
		this.isDeleted = isDeleted;
	}

	public Long getCreatBy() {
		return creatBy;
	}

	public void setCreatBy(Long creatBy) {
		this.creatBy = creatBy;
	}

	public Long getUpdateBy() {
		return updateBy;
	}

	public void setUpdateBy(Long updateBy) {
		this.updateBy = updateBy;
	}

	public Timestamp getCreateOn() {
		return createOn;
	}

	public void setCreateOn(Timestamp createOn) {
		this.createOn = createOn;
	}

	public Timestamp getUpdateOn() {
		return updateOn;
	}

	public void setUpdateOn(Timestamp updateOn) {
		this.updateOn = updateOn;
	}

}
